const fs = require('fs');
const path = require('path');
// 使用gltransition过滤器实现翻页效果
const { FFMPEG_V2_PATH } = require('../src/utils/ffmpeg');

console.log('开始测试翻页效果视频生成功能...\n');

// 确保输出目录存在
const outputDir = './output/videos';
if (!fs.existsSync(outputDir)) {
  fs.mkdirSync(outputDir, { recursive: true });
}

// 测试使用的图片路径（使用jpg文件）
const imagePaths = [
  './input/first-frame.jpg',
  './input/first-frame-long.jpg'
];

// 验证图片文件是否存在
for (const imagePath of imagePaths) {
  if (!fs.existsSync(imagePath)) {
    console.error(`错误: 找不到图片文件 ${imagePath}`);
    process.exit(1);
  }
}

console.log('使用的测试图片:');
imagePaths.forEach((path, index) => {
  console.log(`  ${index + 1}. ${path}`);
});
console.log();

// 测试1: 使用默认参数
console.log('测试1: 使用默认参数生成翻页效果视频...');
const outputPath1 = path.join(outputDir, 'page_flip_default.mp4');
try {
  // 直接构建测试命令，使用gltransition和InvertedPageCurl.glsl实现翻页效果
  const inputParams = imagePaths.map((path, index) => `-loop 1 -i "${path}"`).join(" ");
  
  const filterComplex = "[0:v]scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2,setsar=1,trim=duration=3,setpts=PTS-STARTPTS[v0];" +
                       "[1:v]scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2,setsar=1,trim=duration=3,setpts=PTS-STARTPTS[v1];" +
                       "[v0][v1]gltransition=duration=1:offset=2:source='E\\:/win_build/transitions/InvertedPageCurl.glsl'[out1];" +
                       "[out1]format=yuv420p[vout]";
  
  const command1 = `${FFMPEG_V2_PATH} -y ${inputParams} -analyzeduration 5000000 -probesize 5000000 -filter_complex "${filterComplex}" -map "[vout]" -c:v mpeg4 -q:v 5 -pix_fmt yuv420p -t 5 "${outputPath1}"`;
  
  console.log('生成的FFmpeg命令:');
  console.log(command1);
  console.log();
  
  // 将命令保存到文件中便于查看
  fs.writeFileSync('./temp/command_test1.txt', command1, 'utf-8');
  console.log('✓ 测试1命令生成成功\n');
} catch (error) {
  console.error('✗ 测试1失败:', error.message);
}

// 测试2: 使用自定义参数
console.log('测试2: 使用自定义参数生成翻页效果视频...');
const outputPath2 = path.join(outputDir, 'page_flip_custom.mp4');
try {
  // 直接构建测试命令
  const inputParams = imagePaths.map((path, index) => `-loop 1 -i "${path}"`).join(" ");
  
  const filterComplex = "[0:v]scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2,setsar=1,trim=duration=5,setpts=PTS-STARTPTS[v0];" +
                       "[1:v]scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2,setsar=1,trim=duration=5,setpts=PTS-STARTPTS[v1];" +
                       "[v0][v1]gltransition=duration=2:offset=3:source='E\\:/win_build/transitions/InvertedPageCurl.glsl'[out1];" +
                       "[out1]format=yuv420p[vout]";
  
  const command2 = `${FFMPEG_V2_PATH} -y ${inputParams} -analyzeduration 5000000 -probesize 5000000 -filter_complex "${filterComplex}" -map "[vout]" -c:v mpeg4 -q:v 5 -pix_fmt yuv420p -t 7 "${outputPath2}"`;
  
  console.log('生成的FFmpeg命令:');
  console.log(command2);
  console.log();
  
  // 将命令保存到文件中便于查看
  fs.writeFileSync('./temp/command_test2.txt', command2, 'utf-8');
  console.log('✓ 测试2命令生成成功\n');
} catch (error) {
  console.error('✗ 测试2失败:', error.message);
}

// 测试3: 只使用一张图片
console.log('测试3: 使用单张图片生成视频...');
const outputPath3 = path.join(outputDir, 'single_image.mp4');
try {
  // 直接构建测试命令
  const filterComplex = "[0:v]scale=1920:1080:force_original_aspect_ratio=decrease,pad=1920:1080:(ow-iw)/2:(oh-ih)/2,setsar=1,trim=duration=4,setpts=PTS-STARTPTS[v0];[v0]format=yuv420p[vout]";
  
  const command3 = `${FFMPEG_V2_PATH} -y -loop 1 -i "${imagePaths[0]}" -analyzeduration 5000000 -probesize 5000000 -filter_complex "${filterComplex}" -map "[vout]" -c:v mpeg4 -q:v 5 -pix_fmt yuv420p -t 4 "${outputPath3}"`;
  
  console.log('生成的FFmpeg命令:');
  console.log(command3);
  console.log();
  
  // 将命令保存到文件中便于查看
  fs.writeFileSync('./temp/command_test3.txt', command3, 'utf-8');
  console.log('✓ 测试3命令生成成功\n');
} catch (error) {
  console.error('✗ 测试3失败:', error.message);
}

console.log('所有测试完成！命令已生成并保存到以下文件:');
console.log('  1. ./temp/command_test1.txt (默认参数)');
console.log('  2. ./temp/command_test2.txt (自定义参数)');
console.log('  3. ./temp/command_test3.txt (单张图片)');
console.log();
console.log('输出视频将生成到:');
console.log('  - ./output/videos/page_flip_default.mp4');
console.log('  - ./output/videos/page_flip_custom.mp4');
console.log('  - ./output/videos/single_image.mp4');